package com.control.utils;

import java.util.regex.Pattern;

import android.content.Context;
import android.widget.Toast;

import com.control.constants.Constants.CookerWarn;

/**
 * 
 * @author LY
 * 
 */
public class StringUtils {

	private static Toast toast = null;
	
	public static void ToskShow(Context context,String msg){
		if(isEmpty(msg)){
			return;
		}
		if(toast == null){
			toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
		}
		toast.setText(msg);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setGravity(0, 50, 80);
		toast.show();
		
	}
	
	public static boolean digitCheck(String input) {
	    for(int i = 0; i < input.length(); i++) {
	        char c = input.charAt(i);
	        if( (c < '0' || c > '9') ) {
	        return false;
	        }
	    }
	    return true;
	} 

	public static String replacAllToEntity(String msgBody){
		String msg = msgBody;
		if(msgBody != "" && msgBody != null){
			msg = replaceAll("\\.", ",",msg);
		}
		return msg;
	}
	
	public static String replaceAll(String st1, String st2, String content) {

		return Pattern.compile(st1).matcher(content).replaceAll(st2);
	}
	
	public static String replaceAllBackslash(String content){
		String src = new String();
		if (isEmpty(content)) {
			return content;
		}
		src = replaceAll("\\\\", "/", content);
		return src;
	}
	
	public static boolean isEmpty(String str) {
		return str == null || str.length()<=0 || str.equals("") || str.equals("null");
	}
	
	public static boolean isNotEmpty(String str) {
		return null != str && !"".equals(str) && !"null".equals(str);
	}
	
	/**
	 * 根据字符串截
	 * @param str
	 * @param type
	 * @return
	 */
	public static String subStrIndexOf(String str,String type){
		if(StringUtils.isEmpty(str)){
			return "";
		}
		return str.substring(str.lastIndexOf(type)+1, str.length());
	}
	
	public static String operatorTime(String hour,String minute){
		/*StringBuffer sb = new StringBuffer();
		
		if(isEmpty(hour)){
			sb.append("00");
		}else if(hour.length() == 1){
			sb.append("0"+hour);
		}else if(hour.length() == 2){
			sb.append(hour.toString());
		}
		
		if(isEmpty(minute)){
			sb.append("00");
		}else if(minute.length() == 1){
			sb.append("0"+minute);
		}else if(minute.length() == 2){
			sb.append(minute.toString());
		}*/
		return "00"+hour + minute;
	}
	
	public static String convertWarn(String warn){
		if(isEmpty(warn)){
			return CookerWarn.alarm.getValue();
		}
		if(warn.equals("是")){
			return CookerWarn.alarm.getValue(); 
		}else{
			return CookerWarn.noAlarm.getValue(); 
		}
	}
	/**
	 * 根据长度截取
	 * @param str
	 * @param len
	 * @return
	 */
	public static String subStrLen(String str,int len){
		if(StringUtils.isEmpty(str)){
			return "";
		}
		if(str.length() <= len){
			return str;
		}
		return str.substring(0, len);
	}
	
	
	/**
	 * 给�?添加单引号，方便sql语句in查询
	 * @param st
	 * @param type
	 * @return
	 */
	public static String addSingleQuotes(String st,String type){
		if(isEmpty(st) || isEmpty(type)){
			return "'0'";
		}
		String[] array = st.split(type);
		StringBuffer sb = new StringBuffer();
		for(int i=0,l=array.length;i<l;i++){
			sb.append("'"+array[i]+"',");
		}
		if(sb.toString().endsWith(",")){
			sb.delete(sb.length()-1, sb.length());
		}
		return sb.toString();
	}
	
	/**
	 * 根据类型分割字符
	 * @param st
	 * @param type
	 * @return
	 */
	public static String[] spliteStr(String st,String type){
		if(isEmpty(st) || isEmpty(type)){
			return new String[0];
		}
		return st.split(type);
	}
	
	
	public static void main(String[] args) {
		
	}
}
